﻿using GuanQL.Core.Stores;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using ZhongJia.Core;

namespace GuanQL.DataMigration
{
    public class DatabaseInitializer : IDatabaseInitializer
    {
        private GQLDbContext _dbContext;
        private IServiceProvider _serviceProvider;
        private ILogger _logger;

        public DatabaseInitializer(
               IServiceProvider serviceProvider,
               GQLDbContext dbContext,
               ILoggerFactory loggerFactory)
        {
            _dbContext = dbContext;
            _serviceProvider = serviceProvider;
            _logger = loggerFactory.CreateLogger<DatabaseInitializer>();
        }

        public async Task Seed()
        {
            await _dbContext.Database.EnsureDeletedAsync();
            await _dbContext.Database.EnsureCreatedAsync();
            var seeds = _serviceProvider.GetServices<ISeeder>();
            foreach (var seed in seeds)
            {
                await seed.Seed();
            }
        }
    }

    public interface IDatabaseInitializer
    {
        Task Seed();
    }
}
